#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=131200;
int a[N],b[N],q[N],g[4],T,n,m,k,mul=1,l,mull,ans,f[21][N];
bool d[21][N],vis[N];
char s[N];
void dfs(int x,int pos){
	if (x==0){
		for (int i=1;i<=mull;++i)
			f[0][i]=i;
		int pw=mull;
		for (int i=1;i<=l;++i){
			pw/=2;
			for (int j=1;j<=pw;++j){
				int x=f[i-1][j*2-1],y=f[i-1][j*2];
				if (!d[i][j]){
					if (a[x]>=i) f[i][j]=x;
					else f[i][j]=y;
				}
				else{
					if (a[y]>=i) f[i][j]=y;
					else f[i][j]=x;
				}
			}
		}
		vis[f[l][1]]=1;
		return ;
	}
	for (int i=0;i<=l;++i)
		a[pos]=i,dfs(x-1,pos+1),a[pos]=0;
}
signed main(){
	freopen("arena.in","r",stdin);
	freopen("arena.out","w",stdout);
	scanf("%lld%lld",&n,&m);
	for (int i=1;i<=n;++i)
		scanf("%lld",b+i);
	for (int i=1;i<=m;++i)
		scanf("%lld",q+i);
	while (mul<n) ++k,mul*=2;
	for (int i=1;i<=k;++i){
		mul/=2,scanf("%s",s+1);
		for (int j=1;j<=mul;++j)
            d[i][j]=(s[j]=='1');
	}
	scanf("%lld",&T);
	while (T--){
		for (int i=0;i<4;++i)
			scanf("%lld",g+i);
        ans=0;
		for (int i=1;i<=m;++i){
			l=0,mull=1;
			int sum=0;
			while (mull<q[i]) ++l,mull*=2;
			for (int i=1;i<=mull;++i)
                vis[i]=0;
            for (int j=1;j<=q[i];++j)
                a[j]=b[j]^g[j%4];
			dfs(mull-q[i],q[i]+1);
			for (int j=1;j<=mull;++j)
				if (vis[j]) sum+=j;
			ans^=(i*sum);
		}
		printf("%lld\n",ans);
	}
	return 0;
}
